//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "RandomDefaultModel.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Experimental/Credit/Pool.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (boost::shared_ptr<QuantLib::RandomDefaultModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRandomDefaultModel = NULL;
#endif
	_ppRandomDefaultModel = &childNative;
    
}
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (QuantLib::RandomDefaultModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phRandomDefaultModel = NULL;
#endif
	_ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (&childNative);
    
    _RandomDefaultModelOwner = owner;
    
}

Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (CRandomDefaultModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRandomDefaultModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRandomDefaultModel = NULL;
#endif
	if (!t->IsSubclassOf(CRandomDefaultModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (QuantLib::Handle<QuantLib::RandomDefaultModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRandomDefaultModel = &childNative;
	_ppRandomDefaultModel = &static_cast<boost::shared_ptr<QuantLib::RandomDefaultModel>>(childNative.currentLink());
    
    _RandomDefaultModelOwner = owner;
}
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (QuantLib::Handle<QuantLib::RandomDefaultModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phRandomDefaultModel = &childNative;
	_ppRandomDefaultModel = &static_cast<boost::shared_ptr<QuantLib::RandomDefaultModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CRandomDefaultModel::CRandomDefaultModel (QuantLib::RandomDefaultModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phRandomDefaultModel = NULL;
#endif
	_ppRandomDefaultModel = new boost::shared_ptr<QuantLib::RandomDefaultModel> (new QuantLib::RandomDefaultModel (childNative));
    
}
#endif

Cephei::QL::Experimental::Credit::CRandomDefaultModel::~CRandomDefaultModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppRandomDefaultModel != NULL)
    {
	    delete _ppRandomDefaultModel;
        _ppRandomDefaultModel = NULL;
    }
}
Cephei::QL::Experimental::Credit::CRandomDefaultModel::!CRandomDefaultModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppRandomDefaultModel != NULL)
    {
	    delete _ppRandomDefaultModel;
    }
}
QuantLib::RandomDefaultModel& Cephei::QL::Experimental::Credit::CRandomDefaultModel::GetReference ()
{
    if (_ppRandomDefaultModel == NULL) throw gcnew NativeNullException ();
	return **_ppRandomDefaultModel;
}
boost::shared_ptr<QuantLib::RandomDefaultModel>& Cephei::QL::Experimental::Credit::CRandomDefaultModel::GetShared ()
{
    if (_ppRandomDefaultModel == NULL) throw gcnew NativeNullException ();
	return *_ppRandomDefaultModel;
}
QuantLib::RandomDefaultModel* Cephei::QL::Experimental::Credit::CRandomDefaultModel::GetPointer ()
{
    if (_ppRandomDefaultModel == NULL) throw gcnew NativeNullException ();
	return &**_ppRandomDefaultModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::RandomDefaultModel>& Cephei::QL::Experimental::Credit::CRandomDefaultModel::GetHandle ()
{
	if (_phRandomDefaultModel == NULL)
	{
		_phRandomDefaultModel = new Handle<QuantLib::RandomDefaultModel> (*_ppRandomDefaultModel);
	}
	return *_phRandomDefaultModel;
}
#endif
bool Cephei::QL::Experimental::Credit::CRandomDefaultModel::HasNative () 
{
	return (_ppRandomDefaultModel != NULL);
}

Cephei::QL::Experimental::Credit::IRandomDefaultModel^ Cephei::QL::Experimental::Credit::CRandomDefaultModel::NextSequence (Microsoft::FSharp::Core::FSharpOption<Double>^ tmax)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _tmax = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (tmax) ? (QuantLib::Real)ValueHelper::Convert (tmax->Value) : QL_MAX_REAL); //9a
    	(*_ppRandomDefaultModel)->nextSequence ( _tmax );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Credit::IRandomDefaultModel^ Cephei::QL::Experimental::Credit::CRandomDefaultModel::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppRandomDefaultModel)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

